package com.jeecg.demo.controller;

import com.jeecg.demo.entity.*;
import com.jeecg.demo.service.*;
import net.sf.json.JSONObject;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/vEcpHetinfoController"})
public class VEcpHetinfoController extends BaseController {
  private static final Logger logger = LoggerFactory.getLogger(VEcpHetinfoController.class);
  @Autowired
  private VEcpHetinfoServiceI vEcpHetinfoService;
  @Autowired
  private MtSlDanjtkServiceI mtSlDanjtkService;
  @Autowired
  private MtCoreTiaokServiceI mtCoreTiaokService;
  @Autowired
  private SystemService systemService;
  @Autowired
  private MtHetServiceI mtHetService;
  @Autowired
  private SysAttachmentServiceI sysAttachmentService;
  @Autowired
  private SysAppMessagesenderServiceI sysAppMessagesender;
  @Autowired
  private TradeorderServiceI traderOrderService;
  @Autowired
  private MtHetServiceI mtHetServiceI;
  @Autowired
  private TraderServiceI traderServiceI;
  @Autowired
  private MtTlDanjtkServiceI mtTlDanjtkServiceI;
  @Autowired
  private CmsGlobalCustomServiceI cmsGlobalCustomServiceI;

  @RequestMapping(params={"list"})
  public ModelAndView list(HttpServletRequest request, String type)
  {
    request.setAttribute("type", type);
    return new ModelAndView("com/jeecg/demo/vEcpHetinfoList");
  }

  @RequestMapping(params={"datagrid"})
  public void datagrid(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, String type) throws Exception {
    CriteriaQuery cq = new CriteriaQuery(VEcpHetinfoEntity.class, dataGrid);

    HqlGenerateUtil.installHql(cq, vEcpHetinfo, request.getParameterMap());
    String ctBegin = request.getParameter("createTime_begin");
    String ctEnd = request.getParameter("createTime_end");
    if ("confirm".equals(type))
    {
      cq.eq("zhuangt", "3");
      cq.add();
    }
    if ((StringUtil.isNotEmpty(ctBegin)) && (StringUtil.isNotEmpty(ctEnd)))
    {
      try
      {
        if ("confirm".equals(type))
        {
          cq.eq("zhuangt", "3");
          cq.add();
        }
        cq.ge("createTime", new SimpleDateFormat("yyyy-MM-dd").parse(ctBegin));
        cq.le("createTime", new SimpleDateFormat("yyyy-MM-dd").parse(ctEnd));
      }
      catch (Exception e)
      {
        throw new BusinessException(e.getMessage());
      }
      cq.add();
    }
    else
    {
      cq.add();
    }
    this.vEcpHetinfoService.getDataGridReturn(cq, true);


    StringBuffer stringBuffer = new StringBuffer();
    if (vEcpHetinfo.getChumdwmc() == null) {
      stringBuffer.append(" CHUMDWMC like '%%'");
    }else {
      stringBuffer.append(" CHUMDWMC like '%" + vEcpHetinfo.getChumdwmc() +"%'");
    }
    if (vEcpHetinfo.getMaisdwmc() == null) {
      stringBuffer.append(" and MAISDWMC like '%%'");
    }else {
      stringBuffer.append(" and MAISDWMC like '%" + vEcpHetinfo.getMaisdwmc() + "%'");
    }
    if (!StringUtil.isEmpty(vEcpHetinfo.getIssfzcmmc())) {
      if (vEcpHetinfo.getIssfzcmmc().equals("是")) {
        stringBuffer.append(" and sfzcm = '1'");
      }else {
        stringBuffer.append(" and (sfzcm = '0' or sfzcm is null)");
      }
    }
    if (vEcpHetinfo.getIssfzcmmc() != null) {
      stringBuffer.append(" and qianysj like '%" + vEcpHetinfo.getQianysj() + "%'");
    }
    if (!StringUtil.isEmpty(vEcpHetinfo.getHetbh())) {
      stringBuffer.append(" and hetbh like '%" + vEcpHetinfo.getHetbh() + "%'");
    }
    if (!StringUtil.isEmpty(vEcpHetinfo.getHetlrbh())) {
      stringBuffer.append(" and hetlrbh like '%" + vEcpHetinfo.getHetlrbh() + "%'");
    }
    if (!StringUtil.isEmpty(vEcpHetinfo.getDongzbmmc())) {
      stringBuffer.append(" and dongzbmmc = '" + vEcpHetinfo.getDongzbmmc() + "' ");
    }

    if (!StringUtil.isEmpty(vEcpHetinfo.getSftxmmc())) {
      if (vEcpHetinfo.getSftxmmc().equals("是")) {
        stringBuffer.append(" and sftxm = '1'");
      }else {
        stringBuffer.append(" and (sftxm = '0' or sftxm is null)");
      }
    }


    String sql2 = stringBuffer.toString();


    //String total = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select sum(shul) as ssum from  V_ECP_HETINFO ", new Object[0]).get("ssum"));
    String total = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select round(sum(shul)/10000,2) as ssum from  V_ECP_HETINFO where "+ sql2, new Object[0]).get("ssum"));
    //String price = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select (sum(jiag) + sum(ycjiag)) as price from  V_ECP_HETINFO ", new Object[0]).get("price"));
    String totalprice = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select round( sum((nvl(jiag,0)+ nvl(ycjiag,0)) * shul)/10000,2) as totalprice from  V_ECP_HETINFO  where " + sql2, new Object[0]).get("totalprice"));

    /*String totals = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select sum(shul) as sums from  V_ECP_HETINFO WHERE ZHUANGT = 4 ", new Object[0]).get("sums"));
    String prices = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select (sum(jiag) + sum(ycjiag)) as prices from  V_ECP_HETINFO WHERE ZHUANGT = 4 ", new Object[0]).get("priceS"));*/
    String totals = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select round(sum(shul)/10000,2) as sums from  V_ECP_HETINFO WHERE ZHUANGT = 4  and " + sql2, new Object[0]).get("sums"));
    String prices = String.valueOf(this.vEcpHetinfoService.findOneForJdbc("select round( sum((nvl(jiag,0)+ nvl(ycjiag,0)) * shul)/10000,2) as prices from  V_ECP_HETINFO WHERE ZHUANGT = 4  and " + sql2, new Object[0]).get("priceS"));
    if ("query".equals(type))
    {
      String unit1 = "万元";
      String unit2 = "万吨";
     /* Double tt = Double.valueOf(total);
      Double pp = Double.valueOf(price);
      Double tp = Double.valueOf(tt.doubleValue() * pp.doubleValue());
      DecimalFormat df = new DecimalFormat("#.##");
      if (((tp.doubleValue() >= 10000.0D ? 1 : 0) & (tp.doubleValue() < 1.0E8D ? 1 : 0)) != 0)
      {
        unit1 = "万元";
        tp = Double.valueOf(tp.doubleValue() / 10000.0D);
      }
      if (tp.doubleValue() >= 1.0E8D)
      {
        unit1 = "亿";
        tp = Double.valueOf(tp.doubleValue() / 1.0E8D);
      }*/
      //dataGrid.setFooter("htzt:" + total + unit2 + ",qianysj:" + price + unit1 + ",age,ycjiag:" + "合计:"+",shul:" + totals + unit2 + ",jiag:" + prices + unit1 + ",age,unit:" + "已归档:");
      dataGrid.setFooter("htzt:" + this.formatStr(total) + unit2 + ",qianysj:" + this.formatStr(totalprice) + unit1 + ",age,ycjiag:" + "合计:"+",shul:" + this.formatStr(totals) + unit2 + ",jiag:" + this.formatStr(prices) + unit1 + ",age,unit:" + "已归档:");
    }
    TagUtil.datagrid(response, dataGrid);
  }

  private String formatStr(String str){
    String result = "";
    if (str == "null") {
      result = "0.0";
    }else {
      result = str;
    }
    return result;
  }

  @RequestMapping(params={"doDel"})
  @ResponseBody
  public AjaxJson doDel(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    vEcpHetinfo = (VEcpHetinfoEntity)this.systemService.getEntity(VEcpHetinfoEntity.class, vEcpHetinfo.getId());
    message = "删除成功";
    try
    {
      this.vEcpHetinfoService.delete(vEcpHetinfo);
      this.systemService.addLog(message, Globals.Log_Type_DEL,
        Globals.Log_Leavel_INFO);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message = "删除失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  @RequestMapping(params={"doBatchDel"})
  @ResponseBody
  public AjaxJson doBatchDel(String ids, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    try {
      String[] arrayOfString;
      int j1 = (arrayOfString = ids.split(",")).length;
      for (int i = 0; i < j1; i++) {
        String sysid = arrayOfString[i];
        MtHetEntity mtHet = this.systemService.getEntity(MtHetEntity.class, new Long(sysid));
        if("1".equals(mtHet.getZhuangt())){
          vEcpHetinfoService.delete(mtHet);
          message = "删除成功";
        }else if ("1".equals(mtHet.getZhuangt()) && "2".equals(mtHet.getZhuangt()) || "3".equals(mtHet.getZhuangt()) || "4".equals(mtHet.getZhuangt())){
          message = "删除失败，该合同非起草中的合同或其中有已归档的合同";
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      message = "删除失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  @RequestMapping(params={"doAdd"})
  @ResponseBody
  public AjaxJson doAdd(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "添加成功";
    try
    {
      this.vEcpHetinfoService.save(vEcpHetinfo);
      this.systemService.addLog(message, Globals.Log_Type_INSERT,
        Globals.Log_Leavel_INFO);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message = "添加失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  @RequestMapping(params={"doUpdate"})
  @ResponseBody
  public AjaxJson doUpdate(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "更新成功";
    VEcpHetinfoEntity t = (VEcpHetinfoEntity)this.vEcpHetinfoService.get(VEcpHetinfoEntity.class,
      vEcpHetinfo.getId());
    try
    {
      MyBeanUtils.copyBeanNotNull2Bean(vEcpHetinfo, t);
      this.vEcpHetinfoService.saveOrUpdate(t);
      this.systemService.addLog(message, Globals.Log_Type_UPDATE,
        Globals.Log_Leavel_INFO);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message = "更新失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  @RequestMapping(params={"goAdd"})
  public ModelAndView goAdd(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest req)
  {
    if (StringUtil.isNotEmpty(vEcpHetinfo.getId()))
    {
      vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.getEntity(VEcpHetinfoEntity.class,
        vEcpHetinfo.getId());
      req.setAttribute("vEcpHetinfoPage", vEcpHetinfo);
    }
    return new ModelAndView("com/jeecg/demo/vEcpHetinfo-add");
  }

  @RequestMapping(params={"goUpdate"})
  public ModelAndView goUpdate(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest req)
  {
    if (StringUtil.isNotEmpty(vEcpHetinfo.getId()))
    {
      vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.getEntity(VEcpHetinfoEntity.class,
        vEcpHetinfo.getId());
      req.setAttribute("vEcpHetinfoPage", vEcpHetinfo);
    }
    return new ModelAndView("com/jeecg/demo/vEcpHetinfo-update");
  }

  @RequestMapping(params={"upload"})
  public ModelAndView upload(HttpServletRequest req)
  {
    req.setAttribute("controller_name", "vEcpHetinfoController");
    return new ModelAndView("common/upload/pub_excel_upload");
  }

  @RequestMapping(params={"exportXls"})
  public String exportXls(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, ModelMap modelMap)
  {
    CriteriaQuery cq = new CriteriaQuery(VEcpHetinfoEntity.class, dataGrid);
    HqlGenerateUtil.installHql(cq, vEcpHetinfo, request.getParameterMap());
    List<VEcpHetinfoEntity> vEcpHetinfos = this.vEcpHetinfoService.getListByCriteriaQuery(cq, Boolean.valueOf(false));
    modelMap.put("fileName", "铁路合同查询");
    modelMap.put("entity", VEcpHetinfoEntity.class);
    modelMap.put("params", new ExportParams("铁路合同查询", "导出人" + ResourceUtil.getSessionUser().getRealName(), "导出信息"));
    modelMap.put("data", vEcpHetinfos);
    return "jeecgExcelView";
  }

  @RequestMapping(params={"exportXlsByT"})
  public String exportXlsByT(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, ModelMap modelMap)
  {
    modelMap.put("fileName", "V_ECP_HETINFO");
    modelMap.put("entity", VEcpHetinfoEntity.class);
    modelMap.put("params", new ExportParams(
      "V_ECP_HETINFO列表", "导出人" +
      ResourceUtil.getSessionUser().getRealName(), "导出信息"));
    modelMap.put("data", new ArrayList());
    return "jeecgExcelView";
  }



  @RequestMapping(params={"goConfirm"})
  public ModelAndView confirmPact(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest req, int flag) {
    if (StringUtil.isNotEmpty(vEcpHetinfo.getId()))
    {
      vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.getEntity(VEcpHetinfoEntity.class, vEcpHetinfo.getId());
      req.setAttribute("vEcpHetinfoPage", vEcpHetinfo);
      req.setAttribute("confirmflag", Integer.valueOf(flag));
    }
    CriteriaQuery cq = new CriteriaQuery(MtCoreTiaokEntity.class);
    cq.add();

    /*List<MtCoreTiaokEntity> resultList = this.mtCoreTiaokService.getListByCriteriaQuery(cq, Boolean.valueOf(false));
    req.setAttribute("tiaoKList", resultList);*/
    //修改于2024-05-17
    List<TTlDanjtk> tklist = this.mtTlDanjtkServiceI.findByQueryString("from TTlDanjtk where danjid = '" + vEcpHetinfo.getHetid() + "' order by paix");
    req.setAttribute("tiaoKList", tklist);

    CriteriaQuery cq1 = new CriteriaQuery(SysAttachmentEntity.class);
    cq1.eq("objid", Integer.valueOf(vEcpHetinfo.getHetid()));
    cq1.addOrder("sysid", SortDirection.asc);
    cq1.add();

    String sql = "SELECT * from SYS_ATTACHMENT t WHERE t.OBJURL is not null and t.OBJID=? ORDER BY t.SYSID desc";
    List<Map<String, Object>> sysAttachList = this.systemService.findForJdbc(sql, new Object[] { vEcpHetinfo.getHetid() });
    if (sysAttachList.size() > 3) {
      sysAttachList = sysAttachList.subList(0, 3);
    }
    req.setAttribute("sysAttachList", sysAttachList);
    if(sysAttachList!=null && sysAttachList.size() != 0){
      req.setAttribute("sysAttach", sysAttachList.get(0).get("objurl"));
    }else{
      req.setAttribute("sysAttach","");
    }

    req.setAttribute("prefixUrl", ResourceUtil.getConfigByName("imageServerUrl"));
    return new ModelAndView("com/jeecg/demo/hetquery");
  }

  /**
   * 长协合同审核
   */
  @RequestMapping(params={"doConfirmByCx"}, method={RequestMethod.POST})
  @ResponseBody
  public AjaxJson doConfirmByCx(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "审核成功！";

    CriteriaQuery cq = new CriteriaQuery(MtHetEntity.class);
    cq.eq("hetid", vEcpHetinfo.getHetid());
    cq.add();

    List<MtHetEntity> mtList = this.mtHetService.getListByCriteriaQuery(cq, Boolean.valueOf(false));
    MtHetEntity mt = null;
    if (mtList.size() > 0)
    {
      mt = (MtHetEntity)mtList.get(0);
      try
      {
        this.systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        message = "审核成功";
        this.systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        mt.setZhuangt("4");
        SysAppMessagesenderEntity appmessage = new SysAppMessagesenderEntity();
        appmessage.setSender(Integer.valueOf(0));
        appmessage.setReceiver(Integer.valueOf(vEcpHetinfo.getHetid()));
        appmessage.setOrderid(Integer.valueOf(1));
        appmessage.setMsgstatus("1");
        appmessage.setIsvalid("1");
        appmessage.setAppid("1");
        appmessage.setSendtime(new Date());
        appmessage.setFixedtime(new Date());
        String msg = "您的合同" + vEcpHetinfo.getHetbh() + "已审核,您可到合同列表中查看";
        appmessage.setMsgtitle(msg);
        appmessage.setMsgcontent(msg);
        //mt.setJsstatus(message);
        this.mtHetService.save(mt);

        //新增于2024-03-25
        //1、判断卖方是否是自产煤公司
        String sfZcmgs = "0";   //是否自产煤公司

        //String url = "http://10.2.2.32:8066/jeecg-boot/api/contront/isZcmFirm";
        String url = "";
        List<CmsGlobalCustomEntity>  cms_global_customs = cmsGlobalCustomServiceI.findByQueryString("from CmsGlobalCustomEntity a where a.id = '8066'" );
        if(cms_global_customs != null){
          url = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/isZcmFirm";
        }

        LinkedMultiValueMap params = new LinkedMultiValueMap();
        params.set("zcmFirm", mt.getChumdwmc());
        String ret = this.requestForPost(url, params);
        sfZcmgs = ret;

        //2、如果卖方自产煤公司，推送合同信息至自产煤系统
        if(sfZcmgs.equals("1")) {

          vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.findByQueryString("from VEcpHetglinfoEntity v where v.hetid = '" + vEcpHetinfo.getHetid() + "'").get(0);
          vEcpHetinfo.setSfxs("0");
          //vEcpHetinfo.setHetlrbh("12345678");
          //String url1 = "http://10.2.2.32:8066/jeecg-boot/api/contront/saveHetglinfozc";
          String url1 = "";
          if(cms_global_customs != null){
            url1 = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/saveHetglinfozc";
          }
          LinkedMultiValueMap params1 = new LinkedMultiValueMap();
          JSONObject jsmt1 = JSONObject.fromObject(vEcpHetinfo);
          params1.add("json", jsmt1.toString());
          String ret1 = this.requestForPost(url1, params1);
          JSONObject jsonObject = JSONObject.fromObject(ret1);
          String code = jsonObject.getString("code")==null?"":jsonObject.getString("code");
          if(code.equals("200")){
            mt.setJsstatus("已成功同步！");
          }else if (code.equals("300")){
            message = "内部编号不存在";
            mt.setJsstatus(message);
          }else if (code.equals("301")){
            message = "合同已在自产煤平台产生订单，无法同步！";
            mt.setJsstatus(message);
          }else if (code.equals("500")){
            message = "归档成功,但接口返回异常";
            mt.setJsstatus(message);
          }
        }

        //3、判断买卖双方是否是运销结算
        String sfyxjs = "0";   //是否运销结算
        //String url3 = "http://10.2.2.32:8088/jeecg-boot/api/contront/isMymLessee";
        String url3 ="";
        if(cms_global_customs != null){
          url3 = "http://192.168.0.241:8088" + "jeecg-boot/api/contront/isMymLessee";
        }
        LinkedMultiValueMap params3 = new LinkedMultiValueMap();
        params3.set("chumdwmc", mt.getChumdwmc());
        //params3.set("chumdwmc", "大数据中心");
        params3.set("maisdwmc", mt.getMaisdwmc());
        String ret3 = this.requestForPost(url3, params3);
        sfyxjs = ret3;

        //4、如果卖方自产煤公司，推送合同信息至自产煤系统
        if(sfyxjs.equals("1")) {

          vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.findByQueryString("from VEcpHetglinfoEntity v where v.hetid = '" + vEcpHetinfo.getHetid() + "'").get(0);
          vEcpHetinfo.setSfxs("0");
          //vEcpHetinfo.setHetlrbh("12345678");
          //String url4 = "http://10.2.2.32:8088/jeecg-boot/api/contront/saveHetglinfo";
          String url4 ="";
          if(cms_global_customs != null){
            url4 = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/saveHetglinfo";
          }
          LinkedMultiValueMap params4 = new LinkedMultiValueMap();
          JSONObject jsmt4 = JSONObject.fromObject(vEcpHetinfo);
          params4.add("json", jsmt4.toString());
          String ret4 = this.requestForPost(url4, params4);
          JSONObject jsonObject = JSONObject.fromObject(ret4);
          String code = jsonObject.getString("code")==null?"":jsonObject.getString("code");
          if(code.equals("200")){
            mt.setJsstatus("已成功同步！");
          }else if (code.equals("300")){
            message = "内部编号不存在";
            mt.setJsstatus(message);
          }else if (code.equals("301")){
            message = "合同已在运销结算平台产生订单，无法同步！";
            mt.setJsstatus(message);
          }else if (code.equals("500")){
            message = "归档成功,但接口返回异常";
            mt.setJsstatus(message);
          }
        }


      }
      catch (Exception e)
      {
        e.printStackTrace();
        message = "审核失败";
        throw new BusinessException(e.getMessage());
      }
    }
    j.setMsg(message);
    return j;
  }



  /**
   * 直签合同归档方法
   * @param vEcpHetinfo
   * @param request
   * @return
   */
  @RequestMapping(params={"doConfirm"}, method={RequestMethod.POST})
  @ResponseBody
  public AjaxJson doConfirm(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "更新失败";

    CriteriaQuery cq = new CriteriaQuery(MtHetEntity.class);
    cq.eq("hetid", vEcpHetinfo.getHetid());
    cq.add();




    List<MtHetEntity> mtList = this.mtHetService.getListByCriteriaQuery(cq,Boolean.valueOf(false));
    MtHetEntity mt = null;
    vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.findByQueryString("from VEcpHetinfoEntity v where v.hetid = '" + vEcpHetinfo.getHetid() + "'").get(0);
    Integer price = 0;
    //推送合同质检扣损规则
    CriteriaQuery cqtk = new CriteriaQuery(TTlDanjtk.class);
    cqtk.eq("danjid", vEcpHetinfo.getHetid());
    cqtk.add();

    List<TTlDanjtk> resultList = this.mtCoreTiaokService.getListByCriteriaQuery(cqtk, Boolean.valueOf(false));
    if(resultList != null && resultList.size() != 0){
      String kousdjtk = resultList.get(2).getNeir();
      vEcpHetinfo.setKoudtk(kousdjtk);
    }
    //推送合同质检扣损规则结束
    if (vEcpHetinfo.getDongzbm().equals("33"))
    {
      TradeorderEntity to = new TradeorderEntity();

      to.setAoid(Integer.valueOf(Integer.parseInt("5")));
      to.setTrademodeid(Integer.valueOf(Integer.parseInt("1")));
      to.setPoid(Integer.valueOf(Integer.parseInt("2808")));
      to.setTotime(new Date());
      to.setAotraderid(Integer.valueOf(Integer.parseInt(vEcpHetinfo.getMaisdwbm())));
      to.setAouserid(Integer.valueOf(Integer.parseInt("1")));
      if(vEcpHetinfo.getJiag() != null){
        if(vEcpHetinfo.getJiag().equals(BigDecimal.ZERO)){
          to.setToprice(BigDecimal.ZERO);
        }else{
          to.setToprice(vEcpHetinfo.getJiag());
        }
      }

      to.setToquality(vEcpHetinfo.getShul());
      to.setTostatus("1");

      try
      {
        this.traderOrderService.save(to);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    if (mtList.size() > 0)
    {
      mt = (MtHetEntity)mtList.get(0);
      try
      {
        mt.setZhuangt("4");
        //this.systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        message = "更新成功";
        String url = "";
        if(vEcpHetinfo.getSfzcm()!=null && vEcpHetinfo.getSfzcm().equals("1")){
          List<CmsGlobalCustomEntity>  cms_global_customs = cmsGlobalCustomServiceI.findByQueryString("from CmsGlobalCustomEntity a where a.id = '8066'" );
          if(cms_global_customs != null){
            url = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/saveHetinfozc";
            //url = "http://localhost:8066/jeecg-boot/api/contront/saveHetinfozc";
          }
        }else{
          List<CmsGlobalCustomEntity>  cms_global_customs = cmsGlobalCustomServiceI.findByQueryString("from CmsGlobalCustomEntity a where a.id = '8088'" );
          if(cms_global_customs != null){
            url = cms_global_customs.get(0).getFValue()+"jeecg-boot/api/contront/saveHetinfo";
          }
        }

        RestTemplate template = new RestTemplate();
        LinkedMultiValueMap params = new LinkedMultiValueMap();
        JSONObject jsmt = JSONObject.fromObject(vEcpHetinfo);
        System.out.print(jsmt.toString());
        params.add("json", jsmt.toString());
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<LinkedMultiValueMap> req = new HttpEntity(params, headers);
        String ret = "";
        ret = (String)template.postForObject(url, req, String.class, new Object[0]);
        JSONObject jsonObject = JSONObject.fromObject(ret);
        String code = jsonObject.getString("code")==null?"":jsonObject.getString("code");
        if(code.equals("200")){
          mt.setJsstatus("已成功同步！");
        }else if (code.equals("300")){
          message = "内部编号不存在";
          mt.setJsstatus(message);
        }else if (code.equals("301")){
          message = "合同已在运销结算平台产生订单，无法同步！";
          mt.setJsstatus(message);
        }else if (code.equals("500")){
          message = "归档成功,但接口返回异常";
          mt.setJsstatus(message);
        }
        SysAppMessagesenderEntity appmessage = new SysAppMessagesenderEntity();
        appmessage.setSender(Integer.valueOf(0));
        appmessage.setReceiver(Integer.valueOf(vEcpHetinfo.getHetid()));
        appmessage.setOrderid(Integer.valueOf(1));
        appmessage.setMsgstatus("1");
        appmessage.setIsvalid("1");
        appmessage.setAppid("1");
        appmessage.setSendtime(new Date());
        appmessage.setFixedtime(new Date());

        String msg = "您的合同" + vEcpHetinfo.getHetbh() + "已通过,您可到合同列表中查看";
        appmessage.setMsgtitle(msg);
        appmessage.setMsgcontent(msg);
        //this.sysAppMessagesender.save(appmessage);
        this.mtHetService.saveOrUpdate(mt);
      }
      catch (Exception e)
      {
        mt.setZhuangt("3");
        e.printStackTrace();
        message = "更新失败";
        throw new BusinessException(e.getMessage());
      }
    }
    j.setMsg(message);
    return j;
  }


  /**
   * 交易合同审核，为了解决数据库无法更新的情况
   * @param vEcpHetinfo
   * @param request
   * @return
   */
  @RequestMapping(params={"doConfirmByJiaoY"}, method={RequestMethod.POST})
  @ResponseBody
  public AjaxJson doConfirmByJiaoY(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "审核成功！";

    CriteriaQuery cq = new CriteriaQuery(MtHetEntity.class);
    cq.eq("hetid", vEcpHetinfo.getHetid());
    cq.add();

    List<MtHetEntity> mtList = this.mtHetService.getListByCriteriaQuery(cq, Boolean.valueOf(false));
    MtHetEntity mt = null;
    if (mtList.size() > 0)
    {
      mt = (MtHetEntity)mtList.get(0);
      try
      {
        this.systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        message = "审核成功";
        this.systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        mt.setZhuangt("4");
        SysAppMessagesenderEntity appmessage = new SysAppMessagesenderEntity();
        appmessage.setSender(Integer.valueOf(0));
        appmessage.setReceiver(Integer.valueOf(vEcpHetinfo.getHetid()));
        appmessage.setOrderid(Integer.valueOf(1));
        appmessage.setMsgstatus("1");
        appmessage.setIsvalid("1");
        appmessage.setAppid("1");
        appmessage.setSendtime(new Date());
        appmessage.setFixedtime(new Date());
        String msg = "您的合同" + vEcpHetinfo.getHetbh() + "已审核,您可到合同列表中查看";
        appmessage.setMsgtitle(msg);
        appmessage.setMsgcontent(msg);
        //mt.setJsstatus(message);
        this.mtHetService.save(mt);

        //新增于2024-03-25
        //1、判断卖方是否是自产煤公司
        String sfZcmgs = "0";   //是否自产煤公司

        //String url = "http://10.2.2.32:8066/jeecg-boot/api/contront/isZcmFirm";
        String url = "";
        List<CmsGlobalCustomEntity>  cms_global_customs = cmsGlobalCustomServiceI.findByQueryString("from CmsGlobalCustomEntity a where a.id = '8066'" );
        if(cms_global_customs != null){
          url = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/isZcmFirm";
        }

        LinkedMultiValueMap params = new LinkedMultiValueMap();
        params.set("zcmFirm", mt.getChumdwmc());
        String ret = this.requestForPost(url, params);
        sfZcmgs = ret;

        //2、如果卖方自产煤公司，推送合同信息至自产煤系统
        if(sfZcmgs.equals("1")) {

          vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.findByQueryString("from VEcpHetglinfoEntity v where v.hetid = '" + vEcpHetinfo.getHetid() + "'").get(0);
          vEcpHetinfo.setSfxs("0");
          //vEcpHetinfo.setHetlrbh("12345678");
          //String url1 = "http://10.2.2.32:8066/jeecg-boot/api/contront/saveHetglinfozc";
          String url1 = "";
          if(cms_global_customs != null){
            url1 = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/saveHetglinfozc";
          }
          LinkedMultiValueMap params1 = new LinkedMultiValueMap();
          JSONObject jsmt1 = JSONObject.fromObject(vEcpHetinfo);
          params1.add("json", jsmt1.toString());
          String ret1 = this.requestForPost(url1, params1);
          JSONObject jsonObject = JSONObject.fromObject(ret1);
          String code = jsonObject.getString("code")==null?"":jsonObject.getString("code");
          if(code.equals("200")){
            mt.setJsstatus("已成功同步！");
          }else if (code.equals("300")){
            message = "内部编号不存在";
            mt.setJsstatus(message);
          }else if (code.equals("301")){
            message = "合同已在自产煤平台产生订单，无法同步！";
            mt.setJsstatus(message);
          }else if (code.equals("500")){
            message = "归档成功,但接口返回异常";
            mt.setJsstatus(message);
          }
        }

        //3、判断买卖双方是否是运销结算
        String sfyxjs = "0";   //是否运销结算
        //String url3 = "http://10.2.2.32:8088/jeecg-boot/api/contront/isMymLessee";
        String url3 ="";
        if(cms_global_customs != null){
          url3 = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/isMymLessee";
        }
        LinkedMultiValueMap params3 = new LinkedMultiValueMap();
        params3.set("chumdwmc", mt.getChumdwmc());
        //params3.set("chumdwmc", "大数据中心");
        params3.set("maisdwmc", mt.getMaisdwmc());
        String ret3 = this.requestForPost(url3, params3);
        sfyxjs = ret3;

        //4、如果卖方自产煤公司，推送合同信息至自产煤系统
        if(sfyxjs.equals("1")) {

          vEcpHetinfo = (VEcpHetinfoEntity)this.vEcpHetinfoService.findByQueryString("from VEcpHetglinfoEntity v where v.hetid = '" + vEcpHetinfo.getHetid() + "'").get(0);
          vEcpHetinfo.setSfxs("0");
          //vEcpHetinfo.setHetlrbh("12345678");
          //String url4 = "http://10.2.2.32:8088/jeecg-boot/api/contront/saveHetglinfo";
          String url4 ="";
          if(cms_global_customs != null){
            url4 = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/saveHetglinfo";
          }
          LinkedMultiValueMap params4 = new LinkedMultiValueMap();
          JSONObject jsmt4 = JSONObject.fromObject(vEcpHetinfo);
          params4.add("json", jsmt4.toString());
          String ret4 = this.requestForPost(url4, params4);
          JSONObject jsonObject = JSONObject.fromObject(ret4);
          String code = jsonObject.getString("code")==null?"":jsonObject.getString("code");
          if(code.equals("200")){
            mt.setJsstatus("已成功同步！");
          }else if (code.equals("300")){
            message = "内部编号不存在";
            mt.setJsstatus(message);
          }else if (code.equals("301")){
            message = "合同已在运销结算平台产生订单，无法同步！";
            mt.setJsstatus(message);
          }else if (code.equals("500")){
            message = "归档成功,但接口返回异常";
            mt.setJsstatus(message);
          }
        }


      }
      catch (Exception e)
      {
        e.printStackTrace();
        message = "审核失败";
        throw new BusinessException(e.getMessage());
      }
    }
    j.setMsg(message);
    return j;
  }

  private String requestForPost(String url,  LinkedMultiValueMap params) {
    String result = "";
    RestTemplate template = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
    headers.setContentType(type);
    HttpEntity<LinkedMultiValueMap> req = new HttpEntity(params, headers);
    result = (String)template.postForObject(url, req, String.class, new Object[0]);
    return result;
  }


  @RequestMapping(params={"doBack"}, method={RequestMethod.POST})
  @ResponseBody
  public AjaxJson doBack(VEcpHetinfoEntity vEcpHetinfo, HttpServletRequest request)
  {
    String message = null;
    AjaxJson j = new AjaxJson();
    message = "退回失败";

    CriteriaQuery cq = new CriteriaQuery(MtHetEntity.class);
    cq.eq("hetid", vEcpHetinfo.getHetid());
    cq.add();

    List<MtHetEntity> mtList = this.mtHetService.getListByCriteriaQuery(cq, Boolean.valueOf(false));
    MtHetEntity mt = null;
    if (mtList.size() > 0)
    {
      mt = (MtHetEntity)mtList.get(0);
      try
      {
        this.systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        message = "退回成功";
        String url = "";
//        if(vEcpHetinfo.getSfzcm() != null && vEcpHetinfo.getSfzcm().equals("1")){
//          url = "https://zcm.hncldc.com/jeecg-boot/api/contront/returnHetglinfo";
//        }else{
//          url = "https://yxjsht.hncldc.com/jeecg-boot/api/contront/returnHetglinfo";
//        }

        if(mt.getSfzcm()!=null && mt.getSfzcm().equals("1")){
            List<CmsGlobalCustomEntity>  cms_global_customs = cmsGlobalCustomServiceI.findByQueryString("from CmsGlobalCustomEntity a " +
                    "where a.id = '8066'" );
            if(cms_global_customs != null){
              url = cms_global_customs.get(0).getFValue() + "jeecg-boot/api/contront/returnHetglinfo";
            }
        }else{
            List<CmsGlobalCustomEntity>  cms_global_customs = cmsGlobalCustomServiceI.findByQueryString("from CmsGlobalCustomEntity a " +
                    "where a.id = '8088'" );
            if(cms_global_customs != null){
              url = cms_global_customs.get(0).getFValue()+"jeecg-boot/api/contront/returnHetglinfo";
            }
        }

        RestTemplate template = new RestTemplate();
        LinkedMultiValueMap params = new LinkedMultiValueMap();
        JSONObject jsmt = JSONObject.fromObject(mt);
        params.add("json", jsmt.toString());
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<LinkedMultiValueMap> req = new HttpEntity(params, headers);
        String ret = "";
        ret = (String)template.postForObject(url, req, String.class, new Object[0]);

        JSONObject jsonObject = JSONObject.fromObject(ret);
        String code = jsonObject.getString("code")==null?"":jsonObject.getString("code");
        if (code.equals("200") || code.equals("302")){
          this.systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
          message = "退回并传输成功";
         // if("待定".equals(mt.getFazmch())){
           // this.mtHetService.delete(mt);
          //}else {
            mt.setZhuangt("1");

          //}
          SysAppMessagesenderEntity appmessage = new SysAppMessagesenderEntity();
          appmessage.setSender(Integer.valueOf(0));
          appmessage.setReceiver(Integer.valueOf(vEcpHetinfo.getHetid()));
          appmessage.setOrderid(Integer.valueOf(1));
          appmessage.setMsgstatus("1");
          appmessage.setIsvalid("1");
          appmessage.setAppid("1");
          appmessage.setSendtime(new Date());
          appmessage.setFixedtime(new Date());
          String msg = "您的合同" + vEcpHetinfo.getHetbh() + "已退回,您可到合同列表中查看";
          appmessage.setMsgtitle(msg);
          appmessage.setMsgcontent(msg);
        }else if (code.equals("300")){
          message = "内部编号不存在";
        }else if (code.equals("301") || code.equals("333")){
          message = "合同已在运销结算平台产生订单，无法退回!";
        }else if (code.equals("500")){
          message = "退回失败,接口返回异常";
        }
        mt.setJsstatus(message);
        this.mtHetService.save(mt);
      }
      catch (Exception e)
      {
        e.printStackTrace();
        message = "退回失败";
        throw new BusinessException(e.getMessage());
      }
    }
    j.setMsg(message);
    return j;
  }



  @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  @ResponseBody
  public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
    AjaxJson j = new AjaxJson();

    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
      MultipartFile file = entity.getValue();// 获取上传文件对象
      ImportParams params = new ImportParams();
      params.setTitleRows(2);
      params.setHeadRows(1);
      params.setNeedSave(true);
      try {
        List<TTlTiaok> tks = vEcpHetinfoService.findTiaokInfo("1");
        List<VEcpHetinfoEntity> vecphetinfo = ExcelImportUtil.importExcel(file.getInputStream(), VEcpHetinfoEntity.class,params);
        for (VEcpHetinfoEntity het : vecphetinfo) {
          MtHetEntity mtHetEntity = new MtHetEntity();
          MtHetKzEntity mtHetkzEntity = new MtHetKzEntity();
          //通过数据库序列生成合同编号
          List hetbhlist = vEcpHetinfoService.findListbySql("select  Generate_BianH('ZHQ','1') as htbh from dual");
          if(hetbhlist != null){
            String hetbh = hetbhlist.get(0).toString();
            //mtHetEntity.setHetbh(hetbh);
          }

          mtHetEntity.setHetbh(het.getHetlrbh());
          mtHetEntity.setHetlrbh(het.getHetlrbh());
          //mtHetEntity.setSectorname(het.getSectorname());
          mtHetEntity.setPinp(het.getPinp());
          if(het.getZshetcode() != null){
            mtHetEntity.setZshetcode(het.getZshetcode());
          }else{
            mtHetEntity.setZshetcode("");
          }
          mtHetEntity.setSectorid("6");
          mtHetEntity.setSectorname("煤炭");
          mtHetEntity.setSfzcm(het.getSfzcm());
          mtHetEntity.setMaisdwmc(het.getMaisdwmc());
          String maisdwbm = traderServiceI.getTraderByNameNew(het.getMaisdwmc());
          if(maisdwbm.equals("")){
            j.setMsg("买受人："+het.getMaisdwmc()+"未在平台注册，请完善交易商信息后再进行导入合同！");
            return j;
          }else{
            mtHetEntity.setMaisdwbm(maisdwbm);
          }

          String chumdwbm = traderServiceI.getTraderByNameNew(het.getChumdwmc());
          if(chumdwbm.equals("")){
            j.setMsg("出卖人："+het.getChumdwmc()+"未在平台注册，请完善交易商信息后再进行导入合同！");
            return j;
          }else{
            mtHetEntity.setChumdwbm(chumdwbm);
          }



          mtHetEntity.setMaisdwjsrbm(mtHetEntity.getMaisdwbm());
          mtHetEntity.setMaisdwjsrmc(mtHetEntity.getMaisdwmc());
          mtHetEntity.setChumdwmc(het.getChumdwmc());
          mtHetEntity.setChumdwjsrbm(mtHetEntity.getChumdwbm());
          mtHetEntity.setChumdwjsrmc(mtHetEntity.getChumdwmc());
          mtHetEntity.setChumrssdwbm(mtHetEntity.getChumdwbm());
          mtHetEntity.setChumrssdwmc(mtHetEntity.getChumdwmc());
          mtHetEntity.setMaisrssdwbm(mtHetEntity.getMaisdwbm());
          mtHetEntity.setMaisrssdwmc(mtHetEntity.getMaisdwmc());
          mtHetEntity.setChumrlx("1");
          mtHetEntity.setFazbm(het.getFazbm());
          mtHetEntity.setDaozbm(het.getDaozbm());
          mtHetEntity.setDanw("吨");
          mtHetEntity.setZhuangt("4");
          mtHetEntity.setDongzlx("3");
          mtHetEntity.setCaozr("3002474");
          mtHetEntity.setZhuangt("4");
          mtHetEntity.setFahdwbm(mtHetEntity.getMaisdwbm());
          mtHetEntity.setFahdwmc(mtHetEntity.getMaisdwmc());
          mtHetEntity.setJiaglx("NIE-1");
          mtHetEntity.setDongzbm("33");
          mtHetEntity.setSfxs("0");
          mtHetEntity.setFazbm("待定");
          mtHetEntity.setFazmch("待定");
          mtHetEntity.setDaozmch("待定");
          mtHetEntity.setDaozbm("待定");
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String nowDate = sdf.format(new Date());
          mtHetEntity.setNianf(nowDate);
          mtHetEntity.setQianysj(het.getQianysj());
          if(het.getShul()!=null){
            mtHetkzEntity.setShul(het.getShul().doubleValue());
          }

          List hetidlist = vEcpHetinfoService.findListbySql("SELECT SEQ_TL_HET.nextval as hetid FROM dual");

          if(hetidlist != null){
              String hetid = hetidlist.get(0).toString();
              mtHetEntity.setSysid(Long.parseLong(hetid));
              mtHetEntity.setHetid(hetid);
          }

          mtHetkzEntity.setGengxsj(nowDate);
          mtHetkzEntity.setHetid(mtHetEntity.getHetid());
          mtHetkzEntity.setWorktypeid(het.getJiag().doubleValue());
          mtHetkzEntity.setJiag(0.0);
          mtHetkzEntity.setShouhdwbm(mtHetEntity.getMaisdwbm());
          mtHetkzEntity.setShouhdwmc(mtHetEntity.getMaisdwmc());
          mtHetkzEntity.setHej(het.getJiag().doubleValue());
          mtHetEntity.setDanw("吨");
          mtHetEntity.setSfxs("1");
          mtHetEntity.setYouxqxkssj("2023-01-01");
          mtHetEntity.setYouxqxjssj("2023-12-31");
          mtHetEntity.setZidqysj(het.getQianysj());


          try{
            List<VEcpHetinfoEntity> hetlist = vEcpHetinfoService.findListbySql("select * from v_ecp_hetinfo t where t.hetlrbh = '"+het.getHetlrbh()+"'");
            if(hetlist!=null && hetlist.size() > 0){
              j.setMsg("合同编号重复，编号为:" + het.getHetlrbh());
              continue;
            }else{
                this.vEcpHetinfoService.save(mtHetEntity);
                MtHetEntity hetEntity = (MtHetEntity) vEcpHetinfoService.findByQueryString("from MtHetEntity m where m.hetbh = '"+mtHetEntity.getHetbh()+"'").get(0);
                mtHetkzEntity.setHetid(hetEntity.getHetid());
                List<TTlDanjtk> tkdj = new ArrayList();
                  for (int i = 0; i < tks.size(); i++) {
                    TTlDanjtk djtk = new TTlDanjtk();
                    TTlTiaok tk = tks.get(i);
                    djtk.setDanjid(hetEntity.getHetid());
                    djtk.setGengxsj(new Date());
                    djtk.setBiaot(tk.getBiaot());
                    djtk.setDanjlx("1");
                    djtk.setNeir(tk.getNeir());
                    djtk.setPaix(String.valueOf(i));
                    tkdj.add(djtk);
                    mtTlDanjtkServiceI.save(djtk);
                  }

              mtHetkzEntity.setPinzgg("煤炭,混煤,");
              mtHetkzEntity.setZhilzbjson("[{\"zhibid\":\"529\",\"zhibmc\":\"发热量(Qnet,ar):\",\"xiaxz\":\"\",\"shangxz\":\"5000-5500\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"2\",\"attrobjecttype\":\"1\"},{\"zhibid\":\"530\",\"zhibmc\":\"硫分(St.d):\",\"xiaxz\":\"\",\n" +
                      "   \"shangxz\":\"≤1.5\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"2\",\"attrobjecttype\":\"1\"},{\"zhibid\":\"531\",\n" +
                      "   \"zhibmc\":\"挥发分(Vdaf):\",\"xiaxz\":\"\",\"shangxz\":\"≥24.00\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"2\",\"attrobjecttype\":\"1\"},\n" +
                      "   {\"zhibid\":\"532\",\"zhibmc\":\"全水分(Mt):\",\"xiaxz\":\"\",\"shangxz\":\"≤15.00\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"2\",\"attrobjecttype\":\"1\"},\n" +
                      "   {\"zhibid\":\"533\",\"zhibmc\":\"灰分(Ad):\",\"xiaxz\":\"\",\"shangxz\":\"≤28.00\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"2\",\"attrobjecttype\":\"1\"},\n" +
                      "   {\"zhibid\":\"534\",\"zhibmc\":\"灰熔融性(ST):\",\"xiaxz\":\"\",\"shangxz\":\"\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"2\",\"attrobjecttype\":\"1\"},\n" +
                      "   {\"zhibid\":\"535\",\"zhibmc\":\"哈氏可磨性系数(HGI):\",\"xiaxz\":\"\",\"shangxz\":\"\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"2\",\"attrobjecttype\":\"1\"},\n" +
                      "   {\"zhibid\":\"536\",\"zhibmc\":\"其他:\",\"xiaxz\":\"\",\"shangxz\":\"\",\"jusz1\":\"\",\"jusz2\":\"\",\"danw\":\"\",\"controltype\":\"1\",\"attrobjecttype\":\"1\"}]");
              mtHetkzEntity.setZhilzb("发热量(Qnet,ar):5000-5500硫分(St.d):≤1.5挥发分(Vdaf):≥24.00全水分(Mt):≤15.00灰分(Ad):≤28.00'");

              this.vEcpHetinfoService.save(mtHetkzEntity);
              if(mtHetEntity.getSfzcm() !=null && mtHetEntity.getSfzcm().equals("1") ){

              }else{
                //往运销结算推送数据
                String url = "https://yxjsht.hncldc.com/jeecg-boot/api/contront/saveHetinfo/";
                RestTemplate template = new RestTemplate();
                LinkedMultiValueMap paramsjson = new LinkedMultiValueMap();
                het.setSfxs("1");
                het.setHetbh(mtHetEntity.getHetbh());
                JSONObject jsmt = JSONObject.fromObject(het);
                paramsjson.add("json", jsmt.toString());
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
                headers.setContentType(type);
                HttpEntity<LinkedMultiValueMap> req = new HttpEntity(paramsjson, headers);
                String ret = "";
                ret = (String)template.postForObject(url, req, String.class, new Object[0]);
                JSONObject jsonObject = JSONObject.fromObject(ret);
                String code = jsonObject.getString("code") == null?"":jsonObject.getString("code");
                String message = "传输成功！";
                if(code.equals("200")){
                  mtHetEntity.setJsstatus("已成功同步！");
                }else if (code.equals("300")){
                  message = "内部编号不存在";
                  mtHetEntity.setJsstatus(message);
                }else if (code.equals("301")){
                  message = "合同已在运销结算平台产生订单，无法同步！";
                  mtHetEntity.setJsstatus(message);
                }else if (code.equals("500")){
                  message = "退回失败,接口返回异常";
                  mtHetEntity.setJsstatus(message);
                }
                j.setMsg("文件导入成功！");
              }

              this.vEcpHetinfoService.save(mtHetEntity);
            }
          }catch (Exception e){
            j.setMsg("录入成功,已跳过重复数据");
            e.printStackTrace();
          }
        }
      } catch (Exception e) {
        j.setMsg("文件导入失败,之前可能导入过相同编号的合同或者数据冲突！");
        logger.error(e.getMessage());
      }finally{
        try {
          file.getInputStream().close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return j;
  }
}
